package com.dgut.servicebase.exceptionhandler;

import com.dgut.commonutils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//统一异常处理类
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody//为了能够返回数据
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail().message("统一异常管理！");
    }

    //特定异常管理

    //自定义异常类
    @ExceptionHandler(DoctorException.class)
    @ResponseBody//为了能够返回数据
    public Result doctorError(DoctorException e){
        //将异常保存到文件里面去
        log.error(e.getMsg());
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMessage());
    }
}
